Algorithms

  1. Sorting Algorithms
  2. Searching Algorithms

Data Structure

  1. Array
  2. Linked list
  3. Queue
  4. Stack
  5. Tree


Sorting Algorithms

Complexity(N^2)
Complexity(N log N)
Insertion Sort
Merge Sort
Selection Sort
Quick Sort
Bubble Sort

1. Merge Sort
Merge sort is a recursive algorithm that continually splits a list in half. The key point of it is "divide and conquer". Here's the code:
def mergeSort(alist):
    print("Splitting ",alist)
    if len(alist)>1:
        mid = len(alist)//2
        lefthalf = alist[:mid]
        righthalf = alist[mid:]
 
        mergeSort(lefthalf)
        mergeSort(righthalf)
 
        i=0
        j=0
        k=0
        while i < len(lefthalf) and j < len(righthalf):
            if lefthalf[i] < righthalf[j]:
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1
 
        while i < len(lefthalf):
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1
 
        while j < len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1
    print("Merging ",alist)
 
alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)
2. Quicksort
Steps:
1.pivot
2.partition
3.Quicksort (1, pivot - 1)
4.Quicksort (Pivot + 1, end)
5.Quicksort list (only one element)-sorted, recursion
Here's the code:
def quickSort(alist):
   quickSortHelper(alist,0,len(alist)-1)
 
def quickSortHelper(alist,first,last):
   if first<last:
 
       splitpoint = partition(alist,first,last)
 
       quickSortHelper(alist,first,splitpoint-1)
       quickSortHelper(alist,splitpoint+1,last)
 
 
def partition(alist,first,last):
   pivotvalue = alist[first]
 
   leftmark = first+1
   rightmark = last
 
   done = False
   while not done:
 
       while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
           leftmark = leftmark + 1
 
       while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
           rightmark = rightmark -1
 
       if rightmark < leftmark:
           done = True
       else:
           temp = alist[leftmark]
           alist[leftmark] = alist[rightmark]
           alist[rightmark] = temp
 
   temp = alist[first]
   alist[first] = alist[rightmark]
   alist[rightmark] = temp
 
 
   return rightmark
 
alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)
 
Especially, quick sort is relatively low when processing a part-sorted list, but it is extremely good at sorting and completely random list.

3. Insertion Sort
1. a sorted list and the unsorted list
2. compare, make space and insert
Here's the code:
def insertionSort(alist):
   for index in range(1,len(alist)):
 
     currentvalue = alist[index]
     position = index
 
     while position>0 and alist[position-1]>currentvalue:
         alist[position]=alist[position-1]
         position = position-1
 
     alist[position]=currentvalue
 
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
4. Selection Sort
1. Compare
2. Swap
Here's the code:
def selectionSort(alist):
   for fillslot in range(len(alist)-1,0,-1):
       positionOfMax=0
       for location in range(1,fillslot+1):
           if alist[location]>alist[positionOfMax]:
               positionOfMax = location
 
       temp = alist[fillslot]
       alist[fillslot] = alist[positionOfMax]
       alist[positionOfMax] = temp
 
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
5. Bubble Sorts
1. Two numbers
2. Compare
...
3. The larger to the bottom
Here's the code:
def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp
 
alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)


2. Searching algorithms

1. linear search (waste of time) (not recommended)
Here's the code:
[[code]]
[[code format="python"]]
def sequentialSearch(alist, item):
    pos = 0
    found = False
    while pos < len(alist) and not found:
        if alist[pos] == item:
            found = True
        else:
            pos = pos+1
        return found
 
testlist = [1, 2, 32, 8, 17, 19, 42, 13, 0]
print(sequentialSearch(testlist, 3))
print(sequentialSearch(testlist, 13))

2. Binary Search
Complexity: log2(N)
Key find the middle and cut the half we don't need
Here's the code:
def binarySearch(alist, item):
    if len(alist) ==0
    else:
        midpoint = len(alist)//2
        if alist[midpoint]==item:
        return True
        else:
            if item<alist[midpoint]:
                return binarySearch(alist[:midpoint],item)
            else:
                return binarySearch(alist[midpoint+1:],item)
 
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(binarySearch(testlist, 3))
print(binarySearch(testlist, 13))

3. Data Structure

1.Array (direct access/ continuous/ difficult to insert/remove) (index and content)
Move every content downward by one

2.Linked list
serial access
not continuous
ease to insert/ remove
head-node (data and reference)-tail
head and tail are references (Null)
Insertion:
1.new node
2.new link
3.change link

3.Stack
First in last out
not continuous
Push (insert) and Pop (remove) (check if the stack is already full or empty)
Pointer (top-of-stack-pointer)
Push and Pop: move the pointer
Application:
function main ()
Function1 ()
Function2 ()
Function3 ()
This is the process called "Call stack"
Inversion of a list

4.Queue
First in first out
Tail-in, head-out
ENQUEUE-tail-pointer (move to the next), DEQUEUE-head pointer (move to the next)

5.Tree: (binary tree- left and right)
Root (node with one more reference), Internal node (has parent and children), leaf node (with parent but no children)


Binary Tree
Left and right
5
17
36
Build a tree – insertion